package net.adriana.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import net.adriana.Fac;
import net.adriana.bean.*;
import net.adriana.component.*;

public class FirstWindow extends MyFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private MyList tableList;
	private MyList colList;
	private MyList editList;
	private MyButton next;
	private JTextField changedColNameText = new JTextField();
	private JTextField annoNameText = new JTextField();
	private JTextField annoValueText = new JTextField();
	private JTextField annoPatternText = new JTextField();
	private JTextField annoMessageText = new JTextField();
	private Table selectedTable;
	private Column selectedColumn = null;
	private Column previousSelectedCol = null;
	
	public FirstWindow() {
		super(500, 340);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addAllLabel();
		getTableContain();
		addTexts();
		setVisible(true);
	}


	private void addAllLabel(){
		initLabel("数据库 " + Fac.schema.getSchemaName(), 30, 0, 200, 50);
		initLabel("Table列表", 35, 35, 100, 50);
		initLabel("Column列表", 145, 35, 100, 50);
		initLabel("字段转换成",265, 35, 100, 50);
		initLabel("添加注释", 390, 35, 100, 50);
		initLabel("==>", 235, 150, 50, 30);
		initLabel("@", 410, 70, 30, 30);
		initLabel("value", 400, 115, 50, 30);
		initLabel("pattern", 395, 165, 50, 30);
		initLabel("message", 395, 210, 50, 30);
	}

	private void addTexts(){
		initText(changedColNameText, 260, 240, 90, 20);
		initText(annoNameText, 375, 95, 80, 20);
		initText(annoValueText, 375, 140, 80, 20);
		initText(annoPatternText, 375, 190, 80, 20);
		initText(annoMessageText, 375, 235, 80, 20);
		annoNameText.setEditable(false);
		annoValueText.setEditable(false);
		annoPatternText.setEditable(false);
		annoMessageText.setEditable(false);
		changedColNameText.setEditable(false);
		changedColNameText.addKeyListener(new MyKeyListener(){
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					selectedColumn.setStdColName(changedColNameText.getText());
					editList.setContain(selectedTable.getStdColsName());
				}
			}
		});
		annoNameText.addKeyListener(new MyKeyListener(){
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					selectedColumn.setAnnoName(annoNameText.getText());
				}
			}
		});
//		annoValueText.addKeyListener(new MyKeyListener(){
//			public void keyPressed(KeyEvent e) {
//				if(e.getKeyCode() == KeyEvent.VK_ENTER){
//					selectedColumn.setAnnoValue(annoValueText.getText());
//				}
//			}
//		});
		annoPatternText.addKeyListener(new MyKeyListener(){
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					selectedColumn.setAnnoPattern(annoPatternText.getText());
				}
			}
		});
		annoMessageText.addKeyListener(new MyKeyListener(){
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					selectedColumn.setAnnoMassege(annoMessageText.getText());
				}
			}
		});
	}
	
	private void initText(JTextField text, int x, int y, int width, int height){
		text.setBounds(x, y, width, height);
		content.add(text);
	}
	
	private void getTableContain(){
		tableList = new MyList(Fac.schema.getTablesName());
		content.add(tableList);
		tableList.setLocaAndScroll(content,30, 80, 80, 180);
		tableList.addListSelectionListener(new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if(e.getValueIsAdjusting()){
					int i = tableList.getSelectedIndex();
					selectedTable = Fac.tables[i];
					colList.setContain(selectedTable.getColsName());
					editList.setContain(selectedTable.getStdColsName());
					annoNameText.setEditable(false);
					annoPatternText.setEditable(false);
					annoMessageText.setEditable(false);
					changedColNameText.setEditable(false);
					
					if(selectedColumn != null){
						selectedColumn.setAnnoName(annoNameText.getText());
						selectedColumn.setAnnoValue(annoValueText.getText());
						selectedColumn.setAnnoPattern(annoPatternText.getText());
						selectedColumn.setAnnoMassege(annoMessageText.getText());
						selectedColumn.setStdColName(changedColNameText.getText());
					}
					
				}
			}
		});
		
		colList = new MyList(new String[]{""});
		content.add(colList);
		colList.setLocaAndScroll(content,140, 80, 90, 180);
		colList.addListSelectionListener(new ListSelectionListener() {
			
			public void valueChanged(ListSelectionEvent e) {
				if(e.getValueIsAdjusting()){
					int i = colList.getSelectedIndex();
					resetInformation(i);
					editList.setContain(selectedTable.getStdColsName());
					editList.setSelectedIndex(i);
				}
			}
		});
		
		editList = new MyList(new String[]{""});
		content.add(editList);
		editList.setLocaAndScroll(content,260, 80, 90, 160);
		editList.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				if(e.getValueIsAdjusting()){
					int i = editList.getSelectedIndex();
					resetInformation(i);
					colList.setSelectedIndex(i);
				}
			}
		});
	}
	
	private void resetInformation(int i){
		annoNameText.setEditable(true);
		annoPatternText.setEditable(true);
		annoMessageText.setEditable(true);
		changedColNameText.setEditable(true);
		Column[] cols = selectedTable.getCols();
		if(selectedColumn == null) selectedColumn = cols[i];
		else {
			previousSelectedCol = selectedColumn;
			selectedColumn = cols[i];
		}
		saveAnnotation();
		resetAnnotation();
	}
	
	private void saveAnnotation(){
		if(previousSelectedCol != null){
			previousSelectedCol.setAnnoName(annoNameText.getText());
			previousSelectedCol.setAnnoValue(annoValueText.getText());
			previousSelectedCol.setAnnoPattern(annoPatternText.getText());
			previousSelectedCol.setAnnoMassege(annoMessageText.getText());
			previousSelectedCol.setStdColName(changedColNameText.getText());
		}
	}
	
	private void resetAnnotation(){
		annoNameText.setText(selectedColumn.getAnnoName());
		annoValueText.setText(selectedColumn.getAnnoValue());
		annoPatternText.setText(selectedColumn.getAnnoPattern());
		annoMessageText.setText(selectedColumn.getAnnoMassege());
		changedColNameText.setText(selectedColumn.getStdColName());
	}
	
	public void addNextBtn(){
		next = new MyButton("下一步");
		next.setBounds(335,270,55,20);
		next.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(selectedColumn != null){
					selectedColumn.setAnnoName(annoNameText.getText());
					selectedColumn.setAnnoValue(annoValueText.getText());
					selectedColumn.setAnnoPattern(annoPatternText.getText());
					selectedColumn.setAnnoMassege(annoMessageText.getText());
					selectedColumn.setStdColName(changedColNameText.getText());
				}
				if(Fac.second == null){
					Fac.second = new SecondWindow();
					Fac.currentWindow = Fac.second;
					Fac.second.addPreviousBtn();
					Fac.second.addNextBtn();
					Fac.second.addExitBtn();
				}else Fac.second.setVisible(true);
				Fac.currentWindow = Fac.second;
				Fac.first.setVisible(false);
			}
		});
		content.add(next);
	}
}
